﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BTGISWASECO.BusinessEntities;

/// <summary>
/// Summary description for DMThuocSoHuuDAL
/// </summary>
/// 
namespace BTGISWASECO.DataLayer
{
    public class DMThuocSoHuuDAL
    {
        public DMThuocSoHuuDAL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public DMThuocSoHuuEntities GetDMThuocSoHuuEntitiesByID()
        {
            DMThuocSoHuuEntities DMThuocSoHuu = new DMThuocSoHuuEntities();

            List<DMThuocSoHuuEntities> list = GetList();
            foreach (DMThuocSoHuuEntities b in list)
            {
                DMThuocSoHuu = b;
                break;
            }

            return DMThuocSoHuu;
        }

        public static List<DMThuocSoHuuEntities> GetList()
        {
            DataAccessHelper db = new DataAccessHelper();
            ESRI.ArcGIS.Server.IServerContext serverContext = db.ServerDatabaseSDE();
            List<DMThuocSoHuuEntities> list = new List<DMThuocSoHuuEntities>();
            try
            {
                ESRI.ArcGIS.Geodatabase.IWorkspace workspace;
                DMThuocSoHuuEntities DMThuocSoHuu = new DMThuocSoHuuEntities();
                ESRI.ArcGIS.Geodatabase.IWorkspaceDomains pWSDoms;
                ESRI.ArcGIS.Geodatabase.IDomain pDom;

                ESRI.ArcGIS.esriSystem.IPropertySet propertySet_Connection = (ESRI.ArcGIS.esriSystem.IPropertySet)serverContext.CreateObject("esriSystem.PropertySet");//tham khảo một ví dụ SDE, thay đổi các giá trị này
                propertySet_Connection.SetProperty("server", "localhost");
                propertySet_Connection.SetProperty("instance", "5151");
                propertySet_Connection.SetProperty("database", "BTGISWASECO");
                propertySet_Connection.SetProperty("user", "sde");
                propertySet_Connection.SetProperty("password", "123");
                propertySet_Connection.SetProperty("version", "sde.DEFAULT");
                ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
                ESRI.ArcGIS.Carto.IMapServerObjects mapServerObjects = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
                ESRI.ArcGIS.Carto.IMap aoMap = mapServerObjects.get_Map(mapServer.DefaultMapName);
                ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 workspaceFactory = (ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory");
                //ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
                workspace = workspaceFactory.Open(propertySet_Connection, 0);

                pWSDoms = (ESRI.ArcGIS.Geodatabase.IWorkspaceDomains)workspace;
                pDom = pWSDoms.get_DomainByName("DMThuocSoHuu");
                if (pDom != null)
                {
                    if (pDom.Type == ESRI.ArcGIS.Geodatabase.esriDomainType.esriDTCodedValue)
                    {
                        ESRI.ArcGIS.Geodatabase.ICodedValueDomain codedValueDomain = (ESRI.ArcGIS.Geodatabase.ICodedValueDomain)pDom;
                        for (int i = 0; i < codedValueDomain.CodeCount; i++)
                        {
                            DMThuocSoHuuEntities DMThuocSoHuuL = new DMThuocSoHuuEntities();
                            DMThuocSoHuuL.Ma = Convert.ToInt32(codedValueDomain.get_Value(i));
                            DMThuocSoHuuL.MoTa = codedValueDomain.get_Name(i);
                            list.Add(DMThuocSoHuuL);
                        }
                    }

                    else
                    {
                        ESRI.ArcGIS.Geodatabase.IRangeDomain rangeDomain = (ESRI.ArcGIS.Geodatabase.IRangeDomain)pDom;
                        DMThuocSoHuu.Ma = Convert.ToInt32(rangeDomain.MinValue);
                        DMThuocSoHuu.MoTa = rangeDomain.MaxValue.ToString();
                    }
                }
            }
            catch { list = null; }

            finally
            {

                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
            return list;
        }
    }
}